package com.bianguo.android.apushdemo.view.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.adapter.BaseRecyclerAdapter;
import com.bianguo.android.apushdemo.adapter.MovieAdapter;
import com.bianguo.android.apushdemo.entity.MovieEntity;
import com.bianguo.android.apushdemo.entity.Subject;
import com.bianguo.android.apushdemo.presenter.impl.MoviePresenterImpl;
import com.bianguo.android.apushdemo.utils.DBManager;
import com.bianguo.android.apushdemo.utils.MyLog;
import com.bianguo.android.apushdemo.view.IMovieView;
import com.bianguo.android.apushdemo.view.wiget.popupwindow.BasePopupWindow;
import com.bianguo.android.apushdemo.view.wiget.popupwindow.PopupWindowDialog;
import com.bianguo.android.apushdemo.view.wiget.popupwindow.PopupWindowSelect;
import com.scwang.smartrefresh.header.PhoenixHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MovieActivity extends BaseActivity implements IMovieView, OnRefreshListener, BaseRecyclerAdapter.OnClickItemListener, BasePopupWindow.OnClickBtnListener {

    @BindView(R.id.title_title)
    TextView titleTitleTv;
    @BindView(R.id.movie_refresh_layout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.movie_recycler)
    RecyclerView movieRecycler;
    @BindView(R.id.movie_suspension_layout)
    LinearLayout suspensionLayout;

    private MoviePresenterImpl moviePresenter;
    private ArrayList<Subject> subjects;
    private MovieAdapter movieAdapter;
    private PopupWindowDialog popupWindowDialog;
    private LinearLayoutManager layoutManager;
    private PopupWindowSelect popupWindowSelect;

    @Override
    public int layoutId() {
        return R.layout.activity_movie;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        layoutManager = new LinearLayoutManager(mContext);
        movieRecycler.setLayoutManager(layoutManager);
        mRefreshLayout.setEnableLoadmore(false);
        mRefreshLayout.setRefreshHeader(new PhoenixHeader(mContext));
        subjects = new ArrayList<>();
        movieAdapter = new MovieAdapter(mContext, subjects);
        movieRecycler.setAdapter(movieAdapter);
        moviePresenter = new MoviePresenterImpl(this);
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.autoRefresh();
        movieAdapter.setOnClickItemListener(this);


        movieRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                if (firstVisibleItemPosition >= 1) {
                    suspensionLayout.setVisibility(View.VISIBLE);
                } else {
                    suspensionLayout.setVisibility(View.GONE);
                }
                View view = recyclerView.findChildViewUnder(suspensionLayout.getMeasuredWidth() / 2, suspensionLayout.getMeasuredHeight());
            }
        });
    }

    @OnClick({R.id.title_left_ib, R.id.movie_select_nearby, R.id.movie_select_time,
            R.id.movie_select_type, R.id.movie_select_person})
    public void onClickBk(View view) {
        switch (view.getId()) {
            case R.id.title_left_ib:
                onBackPressed();
                break;
            case R.id.movie_select_nearby:
            case R.id.movie_select_time:
            case R.id.movie_select_type:
            case R.id.movie_select_person:
                showPopupWindowSelect();
                break;
        }
    }

    @Override
    public void showMovieView(MovieEntity movieEntity) {
        ArrayList<Subject> subjects = movieEntity.getSubjects();
        MyLog.mLog("subjects.size() = " + subjects.size());
        movieAdapter.addAll(subjects, true);
        mRefreshLayout.finishRefresh(1000);

//        DBManager.getInstance(mContext).insertMovie(movieEntity);
    }

    @Override
    public void failMovieView() {
        mRefreshLayout.finishRefresh(1000);
        showToast("网络异常");
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        moviePresenter.showMoviePresenter();
    }

    @Override
    public void onClickItem(View view, int position) {
        switch (view.getId()) {
            case R.id.item_select_nearby:
            case R.id.item_select_time:
            case R.id.item_select_type:
            case R.id.item_select_person:
                movieRecycler.scrollToPosition(3);
                showPopupWindowSelect();
                break;
            case R.id.item_image:
                if (2 == position) {
                    startActivity(new Intent(mContext, NewsDetailActivity.class));
                } else {
                    showPopupWindowDialog();
                }
                break;
        }
    }

    private void showPopupWindowDialog() {
        if (popupWindowDialog == null) {
            popupWindowDialog = new PopupWindowDialog(mContext);
            popupWindowDialog.setOnClickBtnListener(this);
        }
        popupWindowDialog.showPopupWindow();
    }

    private void showPopupWindowSelect() {
        if (popupWindowSelect == null) {
//            popupWindowSelect = new PopupWindowSelect(mContext, ViewGroup.LayoutParams.MATCH_PARENT, 900);
            popupWindowSelect = new PopupWindowSelect(mContext, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindowSelect.setOnClickBtnListener(this);
        }
        popupWindowSelect.showPopupWindow(suspensionLayout);
    }

    @Override
    public void onClickBtn(View view) {
        switch (view.getId()) {
            case R.id.popup_dismiss_btn:
            case R.id.popup_sure_btn:
                popupWindowDialog.dismiss();
                break;
            case R.id.popup_select_1:
                showToast("onClick select 1");
                break;
            case R.id.popup_select_2:
                showToast("onClick select 2");
                break;
            case R.id.popup_select_3:
                showToast("onClick select 3");
                break;
            case R.id.popup_select_4:
                showToast("onClick select 4");
                MovieEntity movie = DBManager.getInstance(mContext).getMovie();
                ArrayList<Subject> subjects = movie.getSubjects();
                int size = subjects.size();
                MyLog.mLog("subjects.size() = " + size);
                for (int i = 0; i < size; i++) {
                    Subject subject = subjects.get(i);
                    MyLog.mLog("title = " + subject.getTitle());
                }
                break;
        }
    }
}
